#-*- coding: utf8_*-
from django.shortcuts import render_to_response, redirect
from django.template.context import RequestContext
from django.views.generic.list_detail import object_list
from sap.form import *
from sap.models import *
    
def home(request):
    return render_to_response('index.html')

#*---- Questoes Subjetivas ----*
def create_QuestaoSubjetiva(request):
    if request.method == 'POST':  #DEFNINDO NOSOS MÉTODO POST PARA ENVIO
        form = QuestaoSubjetivaFormulairo(request.POST)
        if form.is_valid():
            subjetiva = QuestaoSubjetiva()
            subjetiva.enunciado = request.POST['enunciado']
            subjetiva.resposta = request.POST['resposta']
            subjetiva.nivel = request.POST['nivel']
            subjetiva.tags = request.POST['tags']
            subjetiva.save()                     

            return render_to_response('menssagem_generica.html', {'msg': u'Questão cadastrada com sucesso'}, context_instance = RequestContext(request))
        return render_to_response('forms_generic.html', {'form':form, 'title': 'Cadastro de Questão'}, context_instance = RequestContext(request))
    form = QuestaoSubjetivaFormulairo()
    return render_to_response('forms_generic.html', {'form':form, 'title': 'Cadastro de Questão'}, context_instance = RequestContext(request))

def generic_list(request, page, query_set, extra_context={}):
    extra_context['search_form'] = SearchForm(query_set)
    
    if request.method == 'POST':
        form = SearchForm(query_set, request.POST)
        if form.is_valid():
            query_set = form.submit_query()
            page=1
    
    return object_list(request=request,
                    queryset=query_set,
                    template_name='lista_geral.html',
                    paginate_by=15,
                    page=page,
                    extra_context = extra_context,
                )

#def list_QuestaoSubjetiva(request, page):
#    objects = QuestaoSubjetiva.objects.filter(status = '1')
#    return generic_list(request, page, objects, extra_context={u'title':'Lista das Questões Subjetivas', 'link':'questao/subjetivas'})